<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">选择宝贝</label>
        <el-select v-model="query.reBaby" filterable placeholder="请选择">
          <el-option
            v-for="item in dict.re_baby"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">评价等级</label>
        <el-select v-model="query.reClass" filterable placeholder="请选择">
          <el-option
            v-for="item in dict.re_class"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">评价人</label>
        <el-input v-model="query.reUser" clearable placeholder="评价人" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="选择宝贝" prop="reBaby">
            <el-select v-model="form.reBaby" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.re_baby"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="评价等级" prop="reClass">
            <el-select v-model="form.reClass" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.re_class"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="评价人" prop="reUser">
            <el-input v-model="form.reUser" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="用户头像">
            <div class="el-upload">
              <img :src="form.reUserImg ? baseApi + '/avatar/' + form.reUserImg : Avatar" class="avatar" @click="toggleShow">
              <myUpload
                v-model="show"
                :headers="headers"
                :url="upImg"
                @crop-upload-success="cropUploadSuccess"
              />
            </div>
            <el-input v-model="form.reUserImg" style="width: 370px;display: none" />
          </el-form-item>
          <el-form-item label="评论内容" prop="reContent">
            <el-input v-model="form.reContent" placeholder="请输入评价内容" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="宝贝图片">
            <div class="el-upload">
              <img :src="form.reBabyImg ? baseApi + '/avatar/' + form.reBabyImg : Avatar" class="avatar" @click="toggleBack">
              <myUpload
                v-model="back"
                :headers="headers"
                :url="upImg"
                @crop-upload-success="cropUp"
              />
            </div>
            <el-input v-model="form.reBabyImg" style="width: 370px;display: none" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="reId" label="评论编号" />
        <el-table-column prop="reBabyImg" label="宝贝图片">
          <template slot-scope="scope">
            <img :src="baseApi + '/avatar/' + scope.row.reBabyImg" style="width: 80px;height: 80px">
          </template>
        </el-table-column>
        <el-table-column prop="reBaby" label="宝贝标题">
          <template slot-scope="scope">
            {{ dict.label.re_baby[scope.row.reBaby] }}
          </template>
        </el-table-column>
        <el-table-column prop="reClass" label="评价等级">
          <template slot-scope="scope">
            {{ dict.label.re_class[scope.row.reClass] }}
          </template>
        </el-table-column>
        <el-table-column prop="reUser" label="评价人" />
        <el-table-column prop="reUserImg" label="用户头像">
          <template slot-scope="scope">
            <img :src="baseApi + '/avatar/' + scope.row.reUserImg" style="width: 80px;height: 80px">
          </template>
        </el-table-column>
        <el-table-column prop="reContent" label="评论内容" />

        <el-table-column prop="rePhone" label="联系方式" />
        <el-table-column prop="reData" label="创建时间" />
        <el-table-column prop="reCode" label="状态" />
        <el-table-column v-if="checkPer(['admin','reviews:edit','reviews:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudReviews from '@/api/reviews'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import Avatar from '@/assets/images/avatar.png'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'
import { mp } from '@/api/gShelves'
import myUpload from 'vue-image-crop-upload'
import store from '@/store'

const defaultForm = { reId: null, reBaby: null, reClass: null, reUser: null, reUserImg: null, reContent: null, reBabyImg: null }
export default {
  name: 'Reviews',
  components: { pagination, crudOperation, rrOperation, udOperation, myUpload },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['re_baby', 're_class'],
  cruds() {
    return CRUD({ title: '测试题2', url: 'api/reviews', idField: 'reId', sort: 'reId,desc', crudMethod: { ...crudReviews }})
  },
  data() {
    return {
      show: false,
      Avatar: Avatar,
      back: false,
      headers: {
        'Authorization': getToken()
      },
      permission: {
        add: ['admin', 'reviews:add'],
        edit: ['admin', 'reviews:edit'],
        del: ['admin', 'reviews:del']
      },
      rules: {
        reBaby: [
          { required: true, message: '选择宝贝不能为空', trigger: 'blur' }
        ],
        reClass: [
          { required: true, message: '评价等级不能为空', trigger: 'blur' }
        ],
        reUser: [
          { required: true, message: '评价人不能为空', trigger: 'blur' }
        ],
        reContent: [
          { required: true, message: '评论内容不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'reBaby', display_name: '选择宝贝' },
        { key: 'reClass', display_name: '评价等级' },
        { key: 'reUser', display_name: '评价人' }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'upImg',
      'baseApi'
    ])
  },
  methods: {
    toggleShow() {
      this.show = !this.show
    },
    toggleBack() {
      this.back = !this.back
    },
    cropUploadSuccess(jsonData, field) {
      store.dispatch('GetInfo').then((data) => {
        mp().then((data) => {
          this.form.reUserImg = data.name
        })
      })
    },
    cropUp(jsonData, field) {
      store.dispatch('GetInfo').then((data) => {
        mp().then((data) => {
          this.form.reBabyImg = data.name
        })
      })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
